Data Visualization - ggplot2

Making graphs in R (II)

Today’s agenda

  • More on ggplot2
  • Faceting graphs using ggplot2
  • Advanced plot customization

Faceting: facet_wrap

  • This is how we ended our first ggplot2 lesson!
## Final plot
gapminder_brazil |> 
  ggplot(aes(x = year, y = lifeExp)) +
  geom_point() +
  labs(title = 'Life expectancy evolution in Brazil',
       subtitle = 'From 1952 to 2007',
       x = 'Years',
       y = 'Life expectancy(age)',
       caption = 'Source: gapminder dataset') +
  theme_light() +
  scale_x_continuous(breaks = seq(1952, 2007, by = 5)) +
  geom_line()

Faceting: facet_wrap

  • What if we need more than one country? (e.g.: Chile)
  • Do we need to copy the whole, paste it and change the country’s name only?
## Plotting the first layer of our plot
gapminder2 <- gapminder |> 
  filter(country %in% c('Brazil', 'Chile'))

## Plotting it!
gapminder2 |> 
  ggplot(aes(x = year, y = lifeExp)) +
  geom_point() +
  labs(title = 'Life expectancy evolution in Brazil & Chile',
       subtitle = 'From 1952 to 2007',
       x = 'Years',
       y = 'Life expectancy(age)',
       caption = 'Source: gapminder dataset') +
  theme_light() +
  scale_x_continuous(breaks = seq(1952, 2007, by = 5)) +
  geom_line() +
  ## We know in advance that our data contains the column named 'country'
  facet_wrap(~country)

gglpot2: themes

  • When using ggplot2, we can:
    • Use predefined themes
    • Make our own basic theme adjustments
    • Add some elements on top of a theme that we liked
  • Before we dive in, let me show you this!
## We can create a ggplot object and keep building on top of it later on!
our_plot <- gapminder_brazil |> 
  ggplot(aes(x = year, y = lifeExp)) +
  geom_point() +
  labs(title = 'Life expectancy evolution in Brazil',
       subtitle = 'From 1952 to 2007',
       x = 'Years',
       y = 'Life expectancy(age)',
       caption = 'Source: gapminder dataset') +
  theme_light() +
  scale_x_continuous(breaks = seq(1952, 2007, by = 5)) +
  geom_line()

Using predefined themes

  • ggplot2 built-in themes
  • The ggthemes package
## Building on top of the ggplot variable we've created
# The `ggthemes` package contains a fair amount of different themes
# Nice way of quickly customizing our plot with one line of code
our_plot +
  # Using a theme from the `ggthemes` package
  theme_economist()

Making small adjustments

  • Some elements: element_text, element_line…
  • Customizing legend and background
## Remember, we are building on top of the ggplot variable we've created
our_plot +
  ## Now, let's make some little improvements
  theme_light() +
  # Setting the position of the legend to be on the `top`
  theme(legend.position = "top",
        ## Adding a rectangular element in the background of the legend
        legend.background = element_rect(fill = "lightblue",
                                         colour = "darkblue",
                                         size = 1,
                                         linetype = "solid"),
        ## Adding a rectangular element in the plot and panel background
        plot.background = element_rect(fill = "lavender"),
        panel.background = element_rect(fill = "ivory"),
        ## Setting the size of the font in the text with element_text
        text = element_text(size = 12),
        ## Bolding the text of the title of the axis with element_text
        axis.title = element_text(face = "bold")) 

Interesting features!

  • Highlighting some aspects of our graph!
## Remember, we are building on top of the ggplot variable we've created
our_plot +
  ## Setting the color of the points to equal a condition (odd years)
  geom_point(aes(color = year %% 2 == 1)) +  
  ## Coloring the result of the condition of black or red
  scale_color_manual(values = c("FALSE" = "black", "TRUE" = "red"), guide = FALSE) +
  ## Adding a text label for the even years
  geom_text(aes(label = ifelse(year %% 2 == 0,
                               as.character(ceiling(lifeExp)),
                               "")), 
            hjust = 0.5, vjust = 2, color = "black", size = 4) 

Lecture recap

  • Faceting graphs in ggplot2
    • facet_wrap function
  • Graph customization
    • Ready to use themes
    • Changing colors, figures, text, etc.

Practice exercises!